/*
 * Copyright (c) 2021 nullptrX
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import 'package:flutter/services.dart';

import 'platform_interface.dart';

/// A [SplashViewPlatformController] that uses a method channel to control the splashview.
class MethodChannelSplashViewPlatform implements SplashViewPlatformController {
  /// Constructs an instance that will listen for webviews broadcasting to the
  /// given [id], using the given [WebViewPlatformCallbacksHandler].
  MethodChannelSplashViewPlatform(int id, this._platformCallbacksHandler)
      : _channel = MethodChannel('${kSplashViewType}_$id')
  {
    _channel.setMethodCallHandler(_handleMethodCall);
  }

  final MethodChannel _channel;

  final SplashAdEventCallback _platformCallbacksHandler;


  Future<void> _handleMethodCall(MethodCall call) async {
    SplashAdEvent? event;
    switch (call.method) {
      case 'onNoAd':
        event = SplashAdEvent.onNoAd;
        break;
      case 'onAdDismiss':
        event = SplashAdEvent.onAdDismiss;
        break;
      case 'onAdClosed':
        event = SplashAdEvent.onAdClosed;
        break;
      case 'onAdPresent':
        event = SplashAdEvent.onAdPresent;
        break;
      case 'onAdExposure':
        event = SplashAdEvent.onAdExposure;
        break;
      case 'onAdLoaded':
        event = SplashAdEvent.onAdLoaded;
        break;
      case 'onAdClicked':
        event = SplashAdEvent.onAdClicked;
        break;
      case 'onAdTick':
        event = SplashAdEvent.onAdTick;
        break;
      case 'onRequestPermissionsFailed':
        event = SplashAdEvent.onRequestPermissionsFailed;
        break;
      case 'onApplicationWillEnterBackground':
        event = SplashAdEvent.onApplicationWillEnterBackground;
        break;
      case 'onAdWillClose':
        event = SplashAdEvent.onAdWillClose;
        break;
      case 'onAdWillPresentFullScreenModal':
        event = SplashAdEvent.onAdWillPresentFullScreenModal;
        break;
      case 'onAdDidPresentFullScreenModal':
        event = SplashAdEvent.onAdDidPresentFullScreenModal;
        break;
      case 'onAdWillDismissFullScreenModal':
        event = SplashAdEvent.onAdWillDismissFullScreenModal;
        break;
      default:
        print('SplashAd unknown event: ${call.method}');
    }
    _platformCallbacksHandler(event, call.arguments);
  }
}
